package com.sdcqjy.property.mode;

import android.content.Context;

import com.google.gson.Gson;
import com.sdcqjy.mylibrary.PrivateFileUtils;
import com.sdcqjy.property.R;

/**
 * Created by 磁磁帅 on 2018/4/15.
 * 功能：
 */

public class CityMode {
    //projectID  1 代表济南  2 代表北京 3代表 上海
    public int id;
    public String name;
    public transient boolean isCheck;

    public CityMode() {
    }

    public CityMode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getLocaIcon() {
        int[] ints = {R.mipmap.img_icon1, R.mipmap.img_icon2, R.mipmap.img_icon3};
        int i = id - 1;
        if (i < 0) i = 0;
        return ints[i % ints.length];
    }

    //-------------保存数据-------------
    private static CityMode mode;
    private static final String jsonName = "CityMode.json";

    public static CityMode getMode(Context context) {
        if (mode == null) {
            try {
                String txt = new PrivateFileUtils(context, jsonName).getString();
                mode = new Gson().fromJson(txt, CityMode.class);
            } catch (Exception e) {
            }
            if (mode == null) mode = new CityMode(1, "山东");
        }
        return mode;
    }

    public static void setMode(Context context, CityMode mode) {
        if (mode == null) return;
        CityMode.mode = mode;
        try {
            String s = new Gson().toJson(mode);
            new PrivateFileUtils(context, jsonName).setString(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void clear(Context context) {
        mode = null;
        new PrivateFileUtils(context, jsonName).delete();
    }
}
